package com.tiantian.homework_3_register.service.impl;

import com.tiantian.homework_3_register.mapper.UserMapper;
import com.tiantian.homework_3_register.pojo.Result;
import com.tiantian.homework_3_register.pojo.User;
import com.tiantian.homework_3_register.service.UserService;
import com.tiantian.homework_3_register.utils.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result register(User user) {
        // 数据加密
        user.setUsername(Base64Util.base64(user.getUsername()));
        user.setPassword(Base64Util.base64(user.getPassword()));
        User u = userMapper.selectUserByUsername(user.getUsername());
        // 判断用户是否已经注册
        if (!ObjectUtils.isEmpty(u)) {
            return Result.error("用户已经注册!");
        }

        if (userMapper.insertUser(user) > 0) {
            return Result.ok("注册成功!");
        }

        return Result.error("注册失败,请重试");
    }
}
